import random

import mysql
import pymysql
import time
import mysql.connector
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.chrome.service import Service

# 配置 Selenium（无头模式，可以不弹出浏览器）
chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-blink-features=AutomationControlled')
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"
)


# 设置 chromedriver 路径
chromedriver_path = "C:\Program Files\Google\Chrome\Application\chromedriver.exe"

# 创建Service对象
service = Service(executable_path=chromedriver_path)

# 创建Options对象（可选）
chrome_options = Options()
# 这里可以添加你需要的启动参数，比如无头模式
# chrome_options.add_argument('--headless')

# 正确创建driver
driver = webdriver.Chrome(service=service, options=chrome_options)

# 连接数据库
connection = mysql.connector.connect(
    host="localhost",  # MySQL服务器地址
    user="root",   # 用户名
    password="123456",  # 密码
    database="bishe"  # 数据库名称
)


try:
    with connection.cursor() as cursor:
        sql = "SELECT href FROM jingdongdiannaoshuju WHERE param_info IS NULL"
        cursor.execute(sql)
        results = cursor.fetchall()

        for idx, row in enumerate(results):
            href = row[0]
            print(f"正在处理第 {idx + 1} 个链接: {href}")
            try:
                driver.get(href)
                time.sleep(random.randint(29, 30))  # 等页面加载一下，适当调整

                try:
                    # 查找 p-parameter
                    p_parameter = driver.find_element(By.CLASS_NAME, 'p-parameter')
                    text = p_parameter.text.strip()
                    cleaned_text = text.replace('更多参数>>', '')
                    param_info = cleaned_text
                    print(f"链接: {href}，成功爬取。")
                except NoSuchElementException:
                    print(f"链接: {href}，未找到p-parameter。")
                    param_info = 'NOT_FOUND'

                # 更新数据库
                update_sql = "UPDATE jingdongdiannaoshuju SET param_info = %s WHERE href = %s"
                cursor.execute(update_sql, (param_info, href))
                connection.commit()

            except TimeoutException:
                print(f"访问 {href} 超时")
            except Exception as e:
                print(f"访问 {href} 出错：{e}")

finally:
    driver.quit()
    connection.close()
